﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Lime49.WPF;
using LockCrypt.Core;

namespace LockCrypt {
    public class Template : TemplateBase, INotifyPropertyChanged {
        public override string ID { 
            get { return base.ID; }
            set { 
                base.ID = value;
                OnPropertyChanged("ID");
            }
        }
        public override string Name {
            get { return base.Name; }
            set {
                base.Name = value;
                OnPropertyChanged("Name");
            }
        }
        public override bool ShowNotes {
            get { return base.ShowNotes; }
            set {
                base.ShowNotes = value;
                OnPropertyChanged("ShowNotes");
            }
        }
        //public override IDictionary<string,IField> Fields {
        public override IList<IField> Fields {
            get { return base.Fields; }
            set {
                base.Fields = value;
                OnPropertyChanged("Fields");
            }
        }
        public override bool CanUserEdit {
            get { return base.CanUserEdit; }
            set {
                base.CanUserEdit = value;
                OnPropertyChanged("CanUserEdit");
            }
        }

        /// <summary>
        /// Initializes a <see cref="Template"/>.
        /// </summary>
        public Template()
            : base() {
            Fields = new SortedObservableCollection<IField>(new Comparers.FieldComparer());
        }

        /// <summary>
        /// Initializes a <see cref="Template"/> from another template.
        /// </summary>
        /// <param name="template">The base template.</param>
        public Template(ITemplate template)
            : this() {
            this.ID = template.ID;
            this.Name = template.Name;
            this.ShowNotes = template.ShowNotes;
            this.CanUserEdit = template.CanUserEdit;
            //foreach(KeyValuePair<string, IField> field in template.Fields) {
                //this.Fields.Add(field.Key, new Field(field.Value));
            foreach(IField field in template.Fields) {
                this.Fields.Add(new Field(field));
            }
        }

        /// <summary>
        /// Creates a deep copy of this template.
        /// </summary>
        /// <returns>A clone of this template.</returns>
        public override ITemplate Clone() {
            Template clone = new Template() {
                CanUserEdit = this.CanUserEdit,
                ID = this.ID,
                Name = this.Name,
                ShowNotes = this.ShowNotes
            };
            foreach(IField field in this.Fields) {
                clone.Fields.Add(new Field(field));
            //foreach(KeyValuePair<string, IField> field in this.Fields) {
              //  clone.Fields.Add(field.Key, field.Value.Clone());
            }
            return clone;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string strPropertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
            }
        }
    }

    
}
